/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.view.systemView;
/**
 * 说明：
 * 角色权限管理
 * @Author：黄盛强
 * @Date：2012-03-23
 */
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.Iterator;
import java.util.Set;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.KeyStroke;

import com.ruipengkj.bean.TEmployee;
import com.ruipengkj.bean.TRole;
import com.ruipengkj.bean.TRoleRight;
import com.ruipengkj.commons.BuildSpringApplication;
import com.ruipengkj.component.JLabelJTextFieldComponent;
import com.ruipengkj.component.RPJButtonBlue;
import com.ruipengkj.component.RPJButtonRoundBlue;
import com.ruipengkj.component.RPJButtonRoundOrager;
import com.ruipengkj.component.RPJButtonWhite;
import com.ruipengkj.component.jpanel.QueryPanel;
import com.ruipengkj.factory.ImageFactory;
import com.ruipengkj.model.SystemManagePrivilegeModel;
import com.ruipengkj.service.TEmployeeService;
import com.ruipengkj.service.TRoleRightService;
import com.ruipengkj.service.TRoleService;
import com.ruipengkj.view.base.BasePagePanel;

public class SystemManagePrivilege  extends BasePagePanel<TRole> implements Runnable{
	private static final long serialVersionUID = 1L;
	
	private JLabelJTextFieldComponent  employeesName;//角色编号
	private JButton selectButton;//查询
	private JButton delButton;//删除
	private JButton addButton;//添加
	private JButton roleButton;//权限分配
	
	private JTable table;
	private JScrollPane tableJsp;
	private SystemManagePrivilegeModel systemManagePrivilegeModel;
	private TRoleService tRoleService;
	private TEmployeeService tEmployeeService;
	private TRoleRightService tRoleRightService;
//	private SystemManagePrivilegeAddDialog systemManagePrivilegeAddDialog;//添加面板
//	private SystemManagePrivilegeModifyDialog systemManagePrivilegeModifyDialog;//修改面板
	private SystemManagePrivilegeEmpAddDialog systemManagePrivilegeEmpAddDialog;//权限分配界面
	public SystemManagePrivilege() {
		tRoleService=BuildSpringApplication.getInstance().getTRoleServiceImpl();
		tEmployeeService=BuildSpringApplication.getInstance().getTEmployeeServiceImpl();
		tRoleRightService=BuildSpringApplication.getInstance().getTRoleRightServiceImpl();
		this.setLayout(new BorderLayout());
		this.init();
		this.initListener();
	}
	private void init() {
		systemManagePrivilegeModel = new SystemManagePrivilegeModel();
		table=uiFactory.createRPJTable(systemManagePrivilegeModel, null, this, 30, JLabel.CENTER);
		tableJsp=uiFactory.createJScrollPane(table);
		table.getTableHeader().setDefaultRenderer(new PrivilegeJTableHeader());
		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		this.add(initTop(), BorderLayout.NORTH);
		this.add(tableJsp, BorderLayout.CENTER);
		this.add(this.downPanel(), BorderLayout.SOUTH);
	}
	
	private JPanel initTop() {
		QueryPanel queryPanel=new QueryPanel("角色权限",ImageFactory.loadImage(ImageFactory.FUN_7_ICON_2));
		JPanel top_panel = uiFactory.createFlowJPanel(FlowLayout.LEFT);
		employeesName = new JLabelJTextFieldComponent("角色名称：", 30);
		selectButton = new RPJButtonBlue("查询");
		roleButton=new RPJButtonWhite("权限分配");
		roleButton.setEnabled(false);
		top_panel.add(employeesName);
		top_panel.add(selectButton);
		top_panel.add(roleButton);
		queryPanel.addPanel(top_panel);
//		queryPanel.addBoxPanel(top_panel);
		return queryPanel;
	}
	private JPanel downPanel(){
		JPanel topPanel=uiFactory.createGridJPanel(1, 2);
		JPanel jp1=uiFactory.createFlowJPanel(FlowLayout.LEFT);
		
		addButton = new RPJButtonRoundBlue("添加(Ctrl+A)");
		delButton = new RPJButtonRoundOrager("删除(Ctrl+D)");
		jp1.add(addButton);
		jp1.add(delButton);
		topPanel.add(jp1);
		return topPanel;
	}
	private void initListener(){
		selectButton.addActionListener(this);
		addButton.addActionListener(this);
		addButton.registerKeyboardAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_A, KeyEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
		delButton.addActionListener(this);
		delButton.registerKeyboardAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_D, KeyEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
		roleButton.addActionListener(this);
	}
	//条件查询
	@Override
	protected void termQuery(final int currentPage){
//		final TRole role=new TRole();
//		role.setName(employeesName.getText().getText().trim());
//		new Thread(){
//			public void run(){
//				tableJsp.getViewport().remove(table);
//				tableJsp.getViewport().add(waitComponent);
//				try {
//					List<TRole> tRoleList = tRoleService.findService(role);
//					if(tRoleList!=null){
//						List<PrivilegePOJO> boolsList = new ArrayList<PrivilegePOJO>();
//						for (int i = 0; i < tRoleList.size(); ++i) {
//							boolean[] boos = new boolean[32];
//							Set<TRoleRight> TRoleRights = tRoleList.get(i).getTRoleRights();
//							Iterator<TRoleRight> it = TRoleRights.iterator();
//							while(it.hasNext()) {
//								TRoleRight tRoleRight = it.next();
//								TRight tRight = tRoleRight.getTRight();
//								boos[ApplicationContext.RIGHT_NUM(tRight.getLevel())] = true;
//							}
//							PrivilegePOJO po = new PrivilegePOJO();
//							po.setBools(boos);
//							boolsList.add(po);
//						}
//						systemManagePrivilegeModel.setRowData(tRoleList);
//						SwingUtilities.invokeLater(new Runnable() {
//							@Override
//							public void run() {
//								tableJsp.getViewport().remove(waitComponent);
//								tableJsp.getViewport().add(table);
//							}
//						});
//					}else{
//						//没有订单数据
//						waitComponent.setText("不存在数据");
//					}
//				} catch (Exception e) {
//					e.printStackTrace();
//				}
//			}
//		}.start();
	}
//	@Override
//	protected PageView<TRole> getPageView() {
//		return pageView;
//	}
	@Override
	public void run() {
		this.termQuery(1);
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==selectButton){
			//查询
			this.termQuery(1);
		}else if(e.getSource()==addButton){
			//添加
//			systemManagePrivilegeAddDialog=new SystemManagePrivilegeAddDialog(null,"添加角色",true);
//			systemManagePrivilegeAddDialog.showDialog();
//			if(systemManagePrivilegeAddDialog.isSuccess){
//				this.termQuery(1);	
//			}
		}else if(e.getSource()==roleButton){
			//权限分配
			int row=table.getSelectedRow();
			if(row!=-1){
				try {
					String id=(String)table.getValueAt(table.getSelectedRow(),0);
    				TRole role=tRoleService.getByIdService(id);
    				if(role==null){
    					JOptionPane.showMessageDialog(null, "不存在此权限");
    					return;
    				}
    				systemManagePrivilegeEmpAddDialog=new SystemManagePrivilegeEmpAddDialog(null,"权限分配",true);
    				systemManagePrivilegeEmpAddDialog.showDialog(role);
				}catch (Exception ee) {
					ee.printStackTrace();
				}
			}else{
				JOptionPane.showMessageDialog(null, "请先选择权限");
			}
		}else if(e.getSource()==delButton){
			//删除
			
			if(table.getSelectedRow()!=-1){
				if(table.getValueAt(table.getSelectedRow(), 1).toString().equals("超级管理员")){
					JOptionPane.showMessageDialog(null, "超级管理员不可被删除");
					return;
				}
				if(JOptionPane.showConfirmDialog(null, "是否删除选中项？")==0){
					String id=(String)table.getValueAt(table.getSelectedRow(),0);
					try {
						TRole role=tRoleService.getByIdService(id);
						if(role!=null){
							//更新员工权限
							Set<TEmployee> set=role.getTEmployees();
							if(set!=null&&set.size()!=0){
								Iterator<TEmployee> it=set.iterator();
								while(it.hasNext()){
									TEmployee emp=it.next();
									emp.setTRole(null);
									tEmployeeService.modifyService(emp);
								}
							}
							//删除权限
							Set<TRoleRight> set2=role.getTRoleRights();
							if(set2!=null&&set2.size()!=0){
								Iterator<TRoleRight> it=set2.iterator();
								while(it.hasNext()){
									TRoleRight rr=it.next();
									tRoleRightService.deleteService(rr.getId());
								}
							}
							tRoleService.deleteService(role.getRoleID());
							JOptionPane.showMessageDialog(null, "操作成功");
							this.termQuery(1);
							return;
						}
					} catch (Exception e1) {
						e1.printStackTrace();
					}
					JOptionPane.showMessageDialog(null, "操作失败");
				}
			} else {
				JOptionPane.showMessageDialog(null, "请先选择需要删除的角色");
			}
		}
	}
//	@Override
//	public void mouseClicked(MouseEvent e) {
//    	if(e.getSource()==table){
//    		roleButton.setEnabled(true);
//    		//表格
//    		if(e.getClickCount() == 2){
//    			if(table.getValueAt(table.getSelectedRow(), 1).toString().equals("超级管理员")){
//    				JOptionPane.showMessageDialog(null, "超级管理员不可被修改");
//    				return;
//    			}
//    			//修改商品
//    			String id=(String)table.getValueAt( table.getSelectedRow(),0);
//    			try {
//    				TRole role=tRoleService.getByIdService(id);
//    				if(role==null){
//    					JOptionPane.showMessageDialog(null, "不存在此权限");
//    					return;
//    				}
//    				systemManagePrivilegeModifyDialog=new SystemManagePrivilegeModifyDialog(null,"修改角色",true);
//        			systemManagePrivilegeModifyDialog.showDialog(role);
//        			if(systemManagePrivilegeModifyDialog.isSuccess){
//        				this.termQuery(1);	
//        			}
//				} catch (Exception e1) {
//					e1.printStackTrace();
//				}
//    		}
//    	}
//    }
	@Override
	public void clear() {
		systemManagePrivilegeModel.clear();
		employeesName.getText().setText("");
		
	}
}
